package com.pzhuweb.sgblog.controller;

//import com.pzhuweb.sgblog.service.LoginService;
import com.pzhuweb.sgblog.domain.entity.User;
import com.pzhuweb.sgblog.domain.enums.AppHttpCodeEnum;
import com.pzhuweb.sgblog.exception.SystemException;
import com.pzhuweb.sgblog.service.AdminLoginService;
import com.pzhuweb.sgblog.service.MenuService;
import com.pzhuweb.sgblog.domain.utils.ResponseResult;
import com.pzhuweb.sgblog.domain.utils.SecurityUtils;
import com.pzhuweb.sgblog.domain.vo.MenuVo;
import com.pzhuweb.sgblog.domain.vo.RoutersVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 鄧浩奇
 * @classname LoginController
 * @description
 * @since 2023/8/6 16:23
 **/

@RestController
@Api(tags = "LoginController",description = "登陆校验")
public class LoginController {

    @Resource
    private AdminLoginService adminLoginService;

    @Resource
    private MenuService menuService;

    @ApiOperation(value = "登陆认证")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user",value = "用户实体类中的账号和密码",dataTypeClass = Object.class,paramType = "body")
    })
    @PostMapping("/user/login")
    public ResponseResult<?> login(@RequestBody User user){
        if(!StringUtils.hasText(user.getUserName())){
            //提示 必须要传用户名
            throw new SystemException(AppHttpCodeEnum.REQUIRE_USERNAME);
        }
        return adminLoginService.login(user);
    }

    @ApiOperation(value = "获取当前登陆用户的信息")
    @GetMapping("getInfo")
    public ResponseResult<?> getInfo() {
        return adminLoginService.getInfo();
    }

    @ApiOperation(value = "获取当前登陆用户对应的路由信息")
    @GetMapping("/getRouters")
    public ResponseResult<RoutersVo> getRouters() {
        List<MenuVo> menuVos = menuService.selectMenuTreeByUserId(SecurityUtils.getUserID());
        return ResponseResult.okResult(new RoutersVo(menuVos));
    }

    @ApiOperation(value = "注销登陆")
    @PostMapping("/user/logout")
    public ResponseResult<?> logout(){
        return adminLoginService.logout();
    }
}
